package com.aihome.springbootshardingjdbc.service.impl;

import com.aihome.springbootshardingjdbc.entity.Member;
import com.aihome.springbootshardingjdbc.mapper.MemberMapper;
import com.aihome.springbootshardingjdbc.query.MemberQuery;
import com.aihome.springbootshardingjdbc.service.MemberService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhangjigang
 * @since 2021-04-27
 */
@Service
@Slf4j
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    /**
     * 添加用户
     *
     * @param member 用户信息
     * @return 主键id
     */
    @Override
    public Integer addMember(Member member) {
        boolean save = this.save(member);
        log.info("添加用户结果:{}", save);
        return member.getId();
    }

    /**
     * 批量新增
     *
     * @param list 批量用户信息
     * @return 添加结果
     */
    @Override
    public Integer addMemberBatch(List<Member> list) {
        boolean b = this.saveBatch(list);
        log.info("批量添加用户结果:{}", b);
        return null;
    }

    /**
     * 修改用户
     *
     * @param member 用户信息
     * @return 主键id
     */
    @Override
    public Integer updateMember(Member member) {
        boolean b = this.saveOrUpdate(member);
        log.info("修改用户结果:{}", b);

//        UpdateWrapper<Member> updateWrapper=new UpdateWrapper<>();
        return member.getId();
    }

    /**
     * 删除用户
     *
     * @param id 主键id
     * @return 主键id
     */
    @Override
    public Integer deleteMember(Integer id) {
        boolean b = this.removeById(id);
        log.info("删除结果:{}", b);
        return id;
    }

    /**
     * 查找单个用户
     *
     * @param member 查询用户条件
     * @return 用户信息
     */
    @Override
    public Member findMember(Member member) {
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>(member);
        Member one = this.getOne(queryWrapper);
        log.info("查询单个用户的结果:{}", one);
        return one;
    }

    /**
     * 查询全部用户
     *
     * @param member 查询用户条件
     * @return 所有用户信息
     */
    @Override
    public List<Member> findMemberForList(Member member) {
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>(member);
        List<Member> list = this.list(queryWrapper);
        log.info("查询单个用户的结果:{}", list);
        return list;
    }

    /**
     * 分页查询用户
     *
     * @param memberQuery 查询条件
     * @return 分页信息
     */
    @Override
    public Page<Member> findMemberForPage(MemberQuery memberQuery) {
        LambdaQueryWrapper<Member> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ge(Member::getCreateTime, memberQuery.getStartCreateTime())
                .lt(Member::getCreateTime, memberQuery.getEndCreateTime());
        Page<Member> page = new Page<>(1, 10);
        Page<Member> page1 = this.page(page, queryWrapper);
        log.info("分页查询结果:{}", page1);
        return page1;
    }

    /**
     * 分页查询pagehelp
     *
     * @param memberQuery 查询条件
     * @return 分页信息
     */
    @Override
    public PageInfo<Member> findMemberForPage2(MemberQuery memberQuery) {
        LambdaQueryWrapper<Member> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ge(Member::getCreateTime, memberQuery.getStartCreateTime())
                .lt(Member::getCreateTime, memberQuery.getEndCreateTime());
        PageHelper.startPage(1, 10);
        List<Member> list = this.list(queryWrapper);
        PageInfo<Member> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
}
